/**
 * Created by Tuffy on 16/1/18.
 */
'use strict';

angular.module('starter.controllers')
.controller('HotelListPublicCtrl', ['$scope','$rootScope', '$state','$ionicSlideBoxDelegate','$ionicLoading','$cordovaToast','HotelService',
    function($scope, $rootScope,$state,$ionicSlideBoxDelegate,$ionicLoading,$cordovaToast,HotelService) {
        $rootScope.isPrivateFlag = false;
        var sendFavFunc = function(){
            //父向子发送收藏刷新广播
            $scope.$broadcast('REFRESH_FAV',true);
        };

        $scope.TAB_HOTEL_INDEX = 0; // 当前tab酒店索引
        $scope.TABS_HOTEL_LIST = []; // tabs酒店列表

    $scope.tabs = [{
        id:'1',
        name:'全部',
        loaded:true,
        templateUrl:'templates/hotel-list-public-all.html',
        controller:'HotelListPublicAllCtrl'
    },{
        id:'2',
        name:'我的收藏',
        templateUrl:'templates/hotel-list-public-favorite.html',
        controller:'HotelListPublicFavoriteCtrl'
    },{
        id:'3',
        name:'新进公海',
        templateUrl:'templates/hotel-list-public-new.html',
        controller:'HotelListPublicNewCtrl'
    }
    //    ,{
    //    id:'4',
    //    name:'附近',
    //    templateUrl:'templates/hotel-list-public-nearby.html',
    //    controller:'HotelListPublicNearbyCtrl'
    //}
    ];
    $scope.onSlideMove = function(data) {
        var _this = $scope.tabs[data.index];
        $scope.TAB_HOTEL_INDEX = data.index;
        if(!_this.loaded){
            _this.loaded = true;
        }
    };


    $scope.showTelList = function($event,hotel) {
        $state.go('app.hotel-contact',{hotelId:hotel.id});
        $event.stopPropagation();
    }
    // 认领
    $scope.doClaim = function($event,hotel) {

        // umeng count
        $rootScope.umengEventObj.onEventById('public_get');

        $ionicLoading.show({
            template: '认领中...'
        });
        HotelService.assign(hotel.id)
            .success(function(res){
                if(res.success){
                    hotel.isClaimed = true; //1:私海,2:公海:
                    $cordovaToast.showShortBottom('认领成功');
                }else{
                    $cordovaToast.showShortBottom('错误原因：'+res.errorMsg);
                }
            })
            .error(function(error){
                $cordovaToast.showShortBottom('认领失败：请重试');
            })
            .finally(function(){
                $ionicLoading.hide();
            });
        $event.stopPropagation();
    };
    // 收藏
    $scope.doFavoriteAdd = function($event,hotel) {

        // umeng count
        $rootScope.umengEventObj.onEventById('public_collect');

        $ionicLoading.show({
            template: '<ion-spinner icon="ios"></ion-spinner>'
        });
        HotelService.favoriteAdd(hotel.id)
            .success(function(res){
                if(res.success){
                    hotel.isFavoriate = 'T'; //1:私海,2:公海:
                    //父向子发送收藏刷新广播
                    sendFavFunc();
                    $cordovaToast.showShortBottom('收藏成功');
                }else{
                    $cordovaToast.showShortBottom('错误原因：'+res.errorMsg);
                }
            })
            .error(function(error){
                $cordovaToast.showShortBottom('收藏失败：请重试');
            })
            .finally(function(){
                $ionicLoading.hide();
            });
        $event.stopPropagation();
    };
    // 取消收藏
    $scope.doFavoriteRemove = function($event,hotel) {

        // umeng count
        $rootScope.umengEventObj.onEventById('public_uncollect');

        $ionicLoading.show({
            template: '<ion-spinner icon="ios"></ion-spinner>'
        });
        HotelService.favoriteRemove(hotel.id)
            .success(function(res){
                if(res.success){
                    hotel.isFavoriate = 'F';
                    //父向子发送收藏刷新广播
                    sendFavFunc();
                    $cordovaToast.showShortBottom('已取消收藏');
                }else{
                    $cordovaToast.showShortBottom('错误原因：'+res.errorMsg);
                }
            })
            .error(function(error){
                $cordovaToast.showShortBottom('取消收藏失败：请重试');
            })
            .finally(function(){
                $ionicLoading.hide();
            });
        $event.stopPropagation();
    };
    //进入酒店详细页
    $scope.goHotelMain = function(hotel) {
        $rootScope.EDIT_HOTEL = hotel; //酒店数据
        $state.go('app.hotel-main',{hotelId:hotel.id});
    };
}])
.controller('HotelListPublicAllCtrl', ['$scope','$rootScope', '$timeout', '$cordovaActionSheet', '$ionicLoading', '$state', 'HotelService','$cordovaToast', 'ionicMaterialMotion', 'ionicMaterialInk',
    function($scope,$rootScope, $timeout, $cordovaActionSheet, $ionicLoading, $state, HotelService,$cordovaToast, ionicMaterialMotion, ionicMaterialInk) {

    $scope.title = '全部';
    $scope.HOTEL_TYPE = 'hotel-list-public-all';
    //筛选
   var filter =  $rootScope.$on($scope.HOTEL_TYPE,function(event,data){
       $scope.hotels.page.pageNum = 1;
       $scope.hotels.search.filter = data;
       $scope.hotels.refresh();
    });
    $scope.$on('$destroy',function(){
        filter();
        if($rootScope.FILTER_DATE && $rootScope.FILTER_DATE[$scope.HOTEL_TYPE]){
            delete $rootScope.FILTER_DATE[$scope.HOTEL_TYPE];
        }
        delete $scope.HOTEL_TYPE;
        delete $rootScope.EDIT_HOTEL;
    });
    $scope.isFilter = function(){
        return  Object.keys($scope.hotels.search.filter).length>0;
    };

    /**
     * hotels
     */
    $scope.hotels ={
        search: {
            value:'',
            filter:{},
            doAct:function(){
                this.moreDataCanBeLoaded = true;
                if(this.timer){
                    $timeout.cancel(this.timer);
                }
                this.timer = $timeout(function(){
                    $scope.hotels.page.pageNum = 1;
                    $scope.hotels.data = [];
                    $scope.hotels.loadData();
                },500);
            }
        },
        page: {
            pageNum:0,
            pageSize:10,
            total:0
        },
        loaded:false,
        data:[],
        loadData:function(okFunc){
            var _this = this,
                params = {
                    hotelName:_this.search.value,
                    pageNum:_this.page.pageNum,
                    pageSize:_this.page.pageSize
                };
            _this.loaded = false;
            for(var key in _this.search.filter){
                params[key] = _this.search.filter[key];
            }
            HotelService.findListPublic(params)
            .success(function(res){
                if(res.success){
                    if(typeof (okFunc) == 'function'){
                        okFunc();
                    }
                    var _data = res.attribute.rows;
                    if(!_data || _data.length<=0){
                        _this.moreDataCanBeLoaded = false;
                        $cordovaToast.showShortBottom('已加载到尾页');
                        return;
                    }
                    _this.data = _this.data.concat(_data);
                    _this.page.total = res.attribute.total;
                }else{
                    _this.page.total = 0;
                    console.log('加载失败');
                }
            }).error(function(error){
                _this.page.total = 0;
                console.log(error);
            }).finally(function(){
                _this.loaded = true;
                if(_this.page.total<_this.page.pageSize){
                    _this.moreDataCanBeLoaded = false;
                }
                $scope.$broadcast('scroll.refreshComplete');
                $scope.$broadcast('scroll.infiniteScrollComplete');
                //$timeout(function() {
                //    ionicMaterialMotion.blinds();
                //}, 200);
                //
                //$timeout(function() {
                //    ionicMaterialInk.displayEffect();
                //}, 1000);
                $scope.TABS_HOTEL_LIST[$scope.TAB_HOTEL_INDEX] = {
                    count: _this.data.length,
                    total: _this.page.total
                };
            });
        },
        moreDataCanBeLoaded:true,
        loadMore:function(){
            this.page.pageNum++;
            this.loadData();
        },
        refresh:function(){
            var _this = this;
            _this.moreDataCanBeLoaded = true;
            _this.page.pageNum = 1;
            _this.loadData(function(){
                _this.data = [];
            });
        }
    } ;
}])
.controller('HotelListPublicFavoriteCtrl', ['$scope','$rootScope', '$timeout', '$cordovaActionSheet', '$ionicLoading', '$state', 'HotelService','$cordovaToast', 'ionicMaterialMotion', 'ionicMaterialInk',
    function($scope,$rootScope, $timeout, $cordovaActionSheet, $ionicLoading, $state, HotelService,$cordovaToast, ionicMaterialMotion, ionicMaterialInk) {

        $scope.title = '收藏';
        $scope.HOTEL_TYPE = 'hotel-list-public-favorite';
        //筛选
        var filter =  $rootScope.$on($scope.HOTEL_TYPE,function(event,data){
            $scope.hotels.page.pageNum = 1;
            $scope.hotels.search.filter = data;
            $scope.hotels.refresh();
        });

        // 收藏
        $scope.doFavoriteAdd = function($event,hotel) {
            $ionicLoading.show({
                template: '<ion-spinner icon="ios"></ion-spinner>'
            });
            HotelService.favoriteAdd(hotel.id)
                .success(function(res){
                    if(res.success){
                        hotel.isFavoriate = 'T'; //1:私海,2:公海:
                        $cordovaToast.showShortBottom('收藏成功');
                    }else{
                        $cordovaToast.showShortBottom('错误原因：'+res.errorMsg);
                    }
                })
                .error(function(error){
                    $cordovaToast.showShortBottom('收藏失败：请重试');
                })
                .finally(function(){
                    $ionicLoading.hide();
                });
            $event.stopPropagation();
        };
        // 取消收藏
        $scope.doFavoriteRemove = function($event,hotel) {
            $ionicLoading.show({
                template: '<ion-spinner icon="ios"></ion-spinner>'
            });
            HotelService.favoriteRemove(hotel.id)
                .success(function(res){
                    if(res.success){
                        hotel.isFavoriate = 'F';
                        $cordovaToast.showShortBottom('已取消收藏');
                    }else{
                        $cordovaToast.showShortBottom('错误原因：'+res.errorMsg);
                    }
                })
                .error(function(error){
                    $cordovaToast.showShortBottom('取消收藏失败：请重试');
                })
                .finally(function(){
                    $ionicLoading.hide();
                });
            $event.stopPropagation();
        };
        $scope.$on('$destroy',function(){
            filter();
            if($rootScope.FILTER_DATE && $rootScope.FILTER_DATE[$scope.HOTEL_TYPE]){
                delete $rootScope.FILTER_DATE[$scope.HOTEL_TYPE];
            }
            delete $scope.HOTEL_TYPE;
            delete $rootScope.EDIT_HOTEL;
        });
        $scope.isFilter = function(){
            return  Object.keys($scope.hotels.search.filter).length>0;
        };
        //接收收藏刷新操作
        $scope.$on('REFRESH_FAV',function(event,msg){
            if(msg){
                $scope.hotels.refresh();
            }
        });
        /**
         * hotels
         */
        $scope.hotels ={
            search: {
                value:'',
                filter:{},
                doAct:function(){
                    if(this.timer){
                        $timeout.cancel(this.timer);
                    }
                    this.timer = $timeout(function(){
                        $scope.hotels.page.pageNum = 1;
                        $scope.hotels.data = [];
                        $scope.hotels.loadData();
                    },500);
                }
            },
            page: {
                pageNum:0,
                pageSize:10,
                total:0
            },
            loaded:false,
            data:[],
            loadData:function(okFunc){
                var _this = this,
                    params = {
                        favorate:'T',
                        hotelName:_this.search.value,
                        pageNum:_this.page.pageNum,
                        pageSize:_this.page.pageSize
                    };
                _this.loaded = false;
                for(var key in _this.search.filter){
                    params[key] = _this.search.filter[key];
                }
                HotelService.findListPublic(params)
                    .success(function(res){
                        if(res.success){
                            if(typeof (okFunc) == 'function'){
                                okFunc();
                            }
                            var _data = res.attribute.rows;
                            if(!_data || _data.length<=0){
                                _this.moreDataCanBeLoaded = false;
                                $cordovaToast.showShortBottom('已加载到尾页');
                                return;
                            }
                            _this.data = _this.data.concat(_data);
                            _this.page.total = res.attribute.total;
                        }else{
                            _this.page.total = 0;
                            console.log('加载失败');
                        }
                    }).error(function(error){
                    _this.page.total = 0;
                    console.log(error);
                }).finally(function(){
                    _this.loaded = true;
                    if(_this.page.total<_this.page.pageSize){
                        _this.moreDataCanBeLoaded = false;
                    }
                    $scope.$broadcast('scroll.refreshComplete');
                    $scope.$broadcast('scroll.infiniteScrollComplete');
                    //$timeout(function() {
                    //    ionicMaterialMotion.blinds();
                    //}, 200);
                    //
                    //$timeout(function() {
                    //    ionicMaterialInk.displayEffect();
                    //}, 1000);
                    $scope.TABS_HOTEL_LIST[$scope.TAB_HOTEL_INDEX] = {
                        count: _this.data.length,
                        total: _this.page.total
                    };
                });
            },
            moreDataCanBeLoaded:true,
            loadMore:function(){
                this.page.pageNum++;
                this.loadData();
            },
            refresh:function(){
                var _this = this;
                _this.page.pageNum = 1;
                _this.loadData(function(){
                    _this.data = [];
                });
            }
        } ;
    }])
.controller('HotelListPublicNewCtrl', ['$scope','$rootScope', '$timeout', '$cordovaActionSheet', '$ionicLoading', '$state', 'HotelService','$cordovaToast', 'ionicMaterialMotion', 'ionicMaterialInk',
    function($scope,$rootScope, $timeout, $cordovaActionSheet, $ionicLoading, $state, HotelService,$cordovaToast, ionicMaterialMotion, ionicMaterialInk) {

        $scope.title = '新进公海';
        $scope.HOTEL_TYPE = 'hotel-list-public-new';
        //筛选
        var filter =  $rootScope.$on($scope.HOTEL_TYPE,function(event,data){
            $scope.hotels.page.pageNum = 1;
            $scope.hotels.search.filter = data;
            $scope.hotels.refresh();
        });
        $scope.$on('$destroy',function(){
            filter();
            if($rootScope.FILTER_DATE && $rootScope.FILTER_DATE[$scope.HOTEL_TYPE]){
                delete $rootScope.FILTER_DATE[$scope.HOTEL_TYPE];
            }
            delete $scope.HOTEL_TYPE;
            delete $rootScope.EDIT_HOTEL;
        });
        $scope.isFilter = function(){
            return  Object.keys($scope.hotels.search.filter).length>0;
        };

        /**
         * hotels
         */
        $scope.hotels ={
            search: {
                value:'',
                filter:{},
                doAct:function(){
                    if(this.timer){
                        $timeout.cancel(this.timer);
                    }
                    this.timer = $timeout(function(){
                        $scope.hotels.page.pageNum = 1;
                        $scope.hotels.data = [];
                        $scope.hotels.loadData();
                    },500);
                }
            },
            page: {
                pageNum:0,
                pageSize:10,
                total:0
            },
            loaded:false,
            data:[],
            loadData:function(okFunc){
                var _this = this,
                    params = {
                        newJoin:'T',
                        hotelName:_this.search.value,
                        pageNum:_this.page.pageNum,
                        pageSize:_this.page.pageSize
                    };
                _this.loaded = false;
                for(var key in _this.search.filter){
                    params[key] = _this.search.filter[key];
                }
                HotelService.findListPublic(params)
                    .success(function(res){
                        if(res.success){
                            if(typeof (okFunc) == 'function'){
                                okFunc();
                            }
                            var _data = res.attribute.rows;
                            if(!_data || _data.length<=0){
                                _this.moreDataCanBeLoaded = false;
                                $cordovaToast.showShortBottom('已加载到尾页');
                                return;
                            }
                            _this.data = _this.data.concat(_data);
                            _this.page.total = res.attribute.total;
                        }else{
                            _this.page.total = 0;
                            console.log('加载失败');
                        }
                    }).error(function(error){
                    _this.page.total = 0;
                    console.log(error);
                }).finally(function(){
                    _this.loaded = true;
                    if(_this.page.total<_this.page.pageSize){
                        _this.moreDataCanBeLoaded = false;
                    }
                    $scope.$broadcast('scroll.refreshComplete');
                    $scope.$broadcast('scroll.infiniteScrollComplete');
                    //$timeout(function() {
                    //    ionicMaterialMotion.blinds();
                    //}, 200);
                    //
                    //$timeout(function() {
                    //    ionicMaterialInk.displayEffect();
                    //}, 1000);
                    $scope.TABS_HOTEL_LIST[$scope.TAB_HOTEL_INDEX] = {
                        count: _this.data.length,
                        total: _this.page.total
                    };
                });
            },
            moreDataCanBeLoaded:true,
            loadMore:function(){
                this.page.pageNum++;
                this.loadData();
            },
            refresh:function(){
                var _this = this;
                _this.page.pageNum = 1;
                _this.loadData(function(){
                    _this.data = [];
                });
            }
        } ;
    }])
.controller('HotelListPublicNearbyCtrl', ['$scope','$rootScope', '$timeout', '$cordovaActionSheet', '$ionicLoading', '$state', 'HotelService','$cordovaToast', 'ionicMaterialMotion', 'ionicMaterialInk',
    function($scope,$rootScope, $timeout, $cordovaActionSheet, $ionicLoading, $state, HotelService,$cordovaToast, ionicMaterialMotion, ionicMaterialInk) {

        $scope.title = '附近';
        $scope.HOTEL_TYPE = 'hotel-list-public-nearby';
        //筛选
        var filter =  $rootScope.$on($scope.HOTEL_TYPE,function(event,data){
            $scope.hotels.page.pageNum = 1;
            $scope.hotels.search.filter = data;
            $scope.hotels.refresh();
        });
        $scope.$on('$destroy',function(){
            filter();
            if($rootScope.FILTER_DATE && $rootScope.FILTER_DATE[$scope.HOTEL_TYPE]){
                delete $rootScope.FILTER_DATE[$scope.HOTEL_TYPE];
            }
            delete $scope.HOTEL_TYPE;
            delete $rootScope.EDIT_HOTEL;
        });
        $scope.isFilter = function(){
            return  Object.keys($scope.hotels.search.filter).length>0;
        };

        /**
         * hotels
         */
        $scope.hotels ={
            search: {
                value:'',
                filter:{},
                doAct:function(){
                    if(this.timer){
                        $timeout.cancel(this.timer);
                    }
                    this.timer = $timeout(function(){
                        $scope.hotels.page.pageNum = 1;
                        $scope.hotels.data = [];
                        $scope.hotels.loadData();
                    },500);
                }
            },
            page: {
                pageNum:0,
                pageSize:10,
                total:0
            },
            loaded:false,
            data:[],
            loadData:function(okFunc){
                var _this = this,
                    params = {
                        hotelName:_this.search.value,
                        pageNum:_this.page.pageNum,
                        pageSize:_this.page.pageSize
                    };
                _this.loaded = false;
                for(var key in _this.search.filter){
                    params[key] = _this.search.filter[key];
                }
                HotelService.findListPublic(params)
                    .success(function(res){
                        if(res.success){
                            if(typeof (okFunc) == 'function'){
                                okFunc();
                            }
                            var _data = res.attribute.rows;
                            if(!_data || _data.length<=0){
                                _this.moreDataCanBeLoaded = false;
                                $cordovaToast.showShortBottom('已加载到尾页');
                                return;
                            }
                            _this.data = _this.data.concat(_data);
                            _this.page.total = res.attribute.total;
                        }else{
                            _this.page.total = 0;
                            console.log('加载失败');
                        }
                    }).error(function(error){
                    _this.page.total = 0;
                    console.log(error);
                }).finally(function(){
                    _this.loaded = true;
                    if(_this.page.total<_this.page.pageSize){
                        _this.moreDataCanBeLoaded = false;
                    }
                    $scope.$broadcast('scroll.refreshComplete');
                    $scope.$broadcast('scroll.infiniteScrollComplete');
                    //$timeout(function() {
                    //    ionicMaterialMotion.blinds();
                    //}, 200);
                    //
                    //$timeout(function() {
                    //    ionicMaterialInk.displayEffect();
                    //}, 1000);
                    $scope.TABS_HOTEL_LIST[$scope.TAB_HOTEL_INDEX] = {
                        count: _this.data.length,
                        total: _this.page.total
                    };
                });
            },
            moreDataCanBeLoaded:true,
            loadMore:function(){
                this.page.pageNum++;
                this.loadData();
            },
            refresh:function(){
                var _this = this;
                _this.page.pageNum = 1;
                _this.loadData(function(){
                    _this.data = [];
                });
            }
        } ;
    }]);
